#include "pch.h"

Background::Background()
{
    x = 0;
    y = 0;
    w = GraphicsDevice::sInstance->getScreenWidth();
    h = GraphicsDevice::sInstance->getScreenHeight();

    mainX = 0;
    mainY = 0;
    mainXExtension = GraphicsDevice::sInstance->getScreenWidth();
    mainYExtension = 0;
    skyBotLeftX = 0;
    skyBotLeftY = GraphicsDevice::sInstance->getScreenHeight() * -1;
    skyBotRightX = GraphicsDevice::sInstance->getScreenWidth();
    skyBotRightY = GraphicsDevice::sInstance->getScreenHeight() * -1;
    skyTopLeftX = 0;
    skyTopLeftY = GraphicsDevice::sInstance->getScreenHeight() * -2;
    skyTopRightX = GraphicsDevice::sInstance->getScreenWidth();
    skyTopRightY = GraphicsDevice::sInstance->getScreenHeight() * -2;

    xVel = 0;
    yVel = 0;

    main = GraphicsDevice::sInstance->load_image("images/bgscroll.jpg");
    mainExtension = GraphicsDevice::sInstance->load_image("images/bgscroll.jpg");
    skyBotLeft = GraphicsDevice::sInstance->load_image("images/bgscrollsky.jpg");
    skyTopLeft = GraphicsDevice::sInstance->load_image("images/bgscrollsky.jpg");
    skyBotRight = GraphicsDevice::sInstance->load_image("images/bgscrollsky.jpg");
    skyTopRight = GraphicsDevice::sInstance->load_image("images/bgscrollsky.jpg");

    scrolling = false;
    inSky = false;
}

Background::~Background()
{
    //dtor
}

void Background::show_object(Projectile* projectile, Cannon* cannon)
{

    GraphicsDevice::sInstance->apply_surface(mainX, mainY, w, h, main, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(mainXExtension, mainYExtension, w, h, mainExtension, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(skyBotLeftX, skyBotLeftY, w, h, skyBotLeft, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(skyBotRightX, skyBotRightY, w, h, skyBotRight, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(skyTopLeftX, skyTopLeftY, w, h, skyTopLeft, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(skyTopRightX, skyTopRightY, w, h, skyTopRight, GraphicsDevice::sInstance->getGameScreen());

    // Check if the backgrounds are already scrolling
    if (scrolling)
    {
        move_object(projectile);
    }

    // Movement on the X-Axis
    if (projectile->getTrueX() > (GraphicsDevice::sInstance->getScreenWidth() / 1.5))
    {
        // Turn on scrolling
        scrolling = true;

        // Set the projectile in the middle of the X-Axis
        projectile->setX(GraphicsDevice::sInstance->getScreenWidth() / 1.5);

        // Movement along the X-Axis
        if (xVel > 0)
        {
            // Move the cannon
            cannon->setX(cannon->getX() - xVel);

            // Move the backgrounds
            mainX = mainX - xVel;
            mainXExtension = mainXExtension - xVel;
            skyBotLeftX = skyBotLeftX - xVel;
            skyBotRightX = skyBotRightX - xVel;
            skyTopLeftX = skyTopLeftX - xVel;
            skyTopRightX = skyTopRightX - xVel;
        }
    }

    // Rotate the Backgrounds on the X-Axis
    if (mainX < GraphicsDevice::sInstance->getScreenWidth() * -1)
    {
        mainX = mainXExtension + GraphicsDevice::sInstance->getScreenWidth();
    }
    if (mainXExtension < GraphicsDevice::sInstance->getScreenWidth() * -1)
    {
        mainXExtension = mainX + GraphicsDevice::sInstance->getScreenWidth();
    }
    if (skyBotLeftX < GraphicsDevice::sInstance->getScreenWidth() * -1)
    {
        skyBotLeftX = skyBotRightX + GraphicsDevice::sInstance->getScreenWidth();
    }
    if (skyBotRightX < GraphicsDevice::sInstance->getScreenWidth() * -1)
    {
        skyBotRightX = skyBotLeftX + GraphicsDevice::sInstance->getScreenWidth();
    }
    if (skyTopLeftX < GraphicsDevice::sInstance->getScreenWidth() * -1)
    {
        skyTopLeftX = skyTopRightX + GraphicsDevice::sInstance->getScreenWidth();
    }
    if (skyTopRightX < GraphicsDevice::sInstance->getScreenWidth() * -1)
    {
        skyTopRightX = skyTopLeftX + GraphicsDevice::sInstance->getScreenWidth();
    }

    // Movement on the Y-Axis
    if (projectile->getTrueY() < (GraphicsDevice::sInstance->getScreenHeight() / 1.5))
    {
        // Turn on scrolling
        scrolling = true;

        // Projectile is past halfway of the screen
        inSky = true;

        // Set the projectile in the middle of the Y-Axis



        // Move the cannon
        cannon->setY(cannon->getY() - yVel);

        // Move the backgrounds along the Y-Axis
        mainY = mainY - yVel;
        mainYExtension = mainYExtension - yVel;
        skyBotLeftY = skyBotLeftY - yVel;
        skyBotRightY = skyBotRightY - yVel;
        skyTopLeftY = skyTopLeftY - yVel;
        skyTopRightY = skyTopRightY - yVel;

    }
    else
    {
        inSky = false;
    }

    // Prevent the Main Backgrounds from going up on the Y-Axis
    if (mainY < 0)
    {
        mainY = 0;
    }
    if (mainYExtension < 0)
    {
        mainYExtension = 0;
    }

    // Return the Sky Backgrounds to the Sky
    if (!inSky)
    {
        skyBotLeftY = GraphicsDevice::sInstance->getScreenHeight() * -1;
        skyBotRightY = GraphicsDevice::sInstance->getScreenHeight() * -1;
        skyTopLeftY = GraphicsDevice::sInstance->getScreenHeight() * -2;
        skyTopRightY = GraphicsDevice::sInstance->getScreenHeight() * -2;
    }
    else
    {
        // Rotate the Sky Backgrounds on the Y-Axis
        if (!projectile->isFalling())
        {
            if (skyBotLeftY > GraphicsDevice::sInstance->getScreenHeight())
            {
                skyBotLeftY = skyTopLeftY - GraphicsDevice::sInstance->getScreenHeight();
            }
            if (skyBotRightY > GraphicsDevice::sInstance->getScreenHeight())
            {
                skyBotRightY = skyTopRightY - GraphicsDevice::sInstance->getScreenHeight();
            }
            if (skyTopLeftY > GraphicsDevice::sInstance->getScreenHeight())
            {
                skyTopLeftY = skyBotLeftY - GraphicsDevice::sInstance->getScreenHeight();
            }
            if (skyTopRightY > GraphicsDevice::sInstance->getScreenHeight())
            {
                skyTopRightY = skyBotRightY - GraphicsDevice::sInstance->getScreenHeight();
            }
        }
        else
        {
            if (skyBotLeftY < GraphicsDevice::sInstance->getScreenHeight() * -1)
            {
                skyBotLeftY = skyTopLeftY + GraphicsDevice::sInstance->getScreenHeight();
            }
            if (skyBotRightY < GraphicsDevice::sInstance->getScreenHeight() * -1)
            {
                skyBotRightY = skyTopRightY + GraphicsDevice::sInstance->getScreenHeight();
            }
            if (skyTopLeftY < GraphicsDevice::sInstance->getScreenHeight() * -1)
            {
                skyTopLeftY = skyBotLeftY + GraphicsDevice::sInstance->getScreenHeight();
            }
            if (skyTopRightY < GraphicsDevice::sInstance->getScreenHeight() * -1)
            {
                skyTopRightY = skyBotRightY + GraphicsDevice::sInstance->getScreenHeight();
            }
        }
    }
}

void Background::move_object(Projectile* projectile)
{
    xVel = projectile->getXVel(); //+ (FPSCounter::sInstance->update.getTicks() / 1000.f);
    yVel = projectile->getYVel(); //+ (FPSCounter::sInstance->update.getTicks() / 1000.f);
}

void Background::clear_surface()
{
    SDL_FreeSurface(main);
    SDL_FreeSurface(mainExtension);
    SDL_FreeSurface(skyBotLeft);
    SDL_FreeSurface(skyBotRight);
    SDL_FreeSurface(skyTopLeft);
    SDL_FreeSurface(skyTopRight);
}

void Background::resetGame()
{
    x = 0;
    y = 0;
    w = GraphicsDevice::sInstance->getScreenWidth();
    h = GraphicsDevice::sInstance->getScreenHeight();

    mainX = 0;
    mainY = 0;
    mainXExtension = GraphicsDevice::sInstance->getScreenWidth();
    mainYExtension = 0;
    skyBotLeftX = 0;
    skyBotLeftY = GraphicsDevice::sInstance->getScreenHeight() * -1;
    skyBotRightX = GraphicsDevice::sInstance->getScreenWidth();
    skyBotRightY = GraphicsDevice::sInstance->getScreenHeight() * -1;
    skyTopLeftX = 0;
    skyTopLeftY = GraphicsDevice::sInstance->getScreenHeight() * -2;
    skyTopRightX = GraphicsDevice::sInstance->getScreenWidth();
    skyTopRightY = GraphicsDevice::sInstance->getScreenHeight() * -2;

    xVel = 0;
    yVel = 0;
}
